package com.iptv.grape.ui.control.ad;

import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;

import com.iptv.grape.R;
import com.iptv.grape.bu.GrapeContext;
import com.iptv.grape.domain.Channel;
import com.iptv.grape.domain.IListViewShowItem;
import com.iptv.grape.util.ToastUtil;
import com.iptv.grape.view.MarqueeTextView;
import com.iptv.grape.view.ListViewVScroll;
import com.iptv.grape.util.AppLog;

import java.util.List;

// 5级联动菜单时间监听者
public class ListViewListener implements View.OnFocusChangeListener, ListViewVScroll.IListViewVScroll {

    private ListViewUI listViewUI;

    public ListViewListener(ListViewUI listViewUI) {
        this.listViewUI = listViewUI;
    }
//    private String lastStyle ="topMenuListItem";
//    private ListViewVScroll lastLV =null==listViewUI?null:listViewUI.getListViewVScroll();
    private int lastItem = -1;
    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        switch (this.listViewUI.getCurStyle().getLayoutFileName()) {
            case "topMenuListItem":
                onFocusChangeHelp(view, hasFocus);
                break;
            case "channelTypeListItem":
                onFocusChangeHelp(view, hasFocus);
                break;
            case "channelListItem":
                onChannelListItem(view, hasFocus);
                break;
            case "billAppointment":
                onChannelBillListItem(view, hasFocus);
                break;
            case "channelBillListItem":
                onChannelBillListItem(view, hasFocus);
                break;
        }

        if (hasFocus) {
            listViewUI.getMian().setCurFocusIndex(listViewUI.getIndex());
            onItemMove(view, listViewUI.getListViewVScroll().getFocusIndex());

        }else{
            if (lastItem!=-1)
                openMarquee(listViewUI,lastItem,false);
        }
    }
    private void openMarquee(ListViewUI lasLvUI,int item,boolean isOpen){
        RelativeLayout lastLayout = (RelativeLayout) lasLvUI.getListViewVScroll().getChildAt(item);// 获得子item的layout
        if (lastLayout == null) {
            AppLog.getCurLog().e("===>>>  未找到该项View " );
            return;
        }
        String style = lasLvUI.getCurStyle().getLayoutFileName();
        MarqueeTextView tvView=null;
        switch (style) {
            case "topMenuListItem":
            case "channelTypeListItem":
            case "channelListItem":
                 tvView = lastLayout.findViewById(R.id.show);
                break;
            case "channelBillListItem":
            case "billAppointment":
                 tvView = lastLayout.findViewById(R.id.tv_item_tip2);

                break;
        }
        if(null!=tvView)
            if(isOpen) {
                tvView.startFromHead();
            }else{
                tvView.stopScroll();
            }
    }

    private void onFocusChangeHelp(View view, boolean hasFocus) {
        if (hasFocus) {
            listViewUI.getImageView().setAlpha(1.0f);
            listViewUI.getImageView().setBackgroundResource(R.drawable.img_fucs);
            listViewUI.getRelativeLayout().setBackgroundResource(R.drawable.img_fucs_rel);
        } else {
            listViewUI.getImageView().setAlpha(0.4f);
            listViewUI.getImageView().setBackgroundResource(R.drawable.img_blus);
            listViewUI.getRelativeLayout().setBackgroundResource(R.mipmap.relbackblus);
        }
    }


    private void onChannelBillListItem(View view, boolean hasFocus) {
        if (hasFocus) {
            listViewUI.getImageView().setAlpha(1.0f);
            listViewUI.getImageView().setBackgroundResource(R.drawable.img_fucs);
        } else {
            listViewUI.getImageView().setAlpha(0.4f);
            listViewUI.getImageView().setBackgroundResource(R.color.backimgblus);

        }
    }

    private void onChannelListItem(View view, boolean hasFocus) {
        if (hasFocus) {
            listViewUI.getImageView().setAlpha(1.0f);
            listViewUI.getImageView().setBackgroundResource(R.drawable.img_fucs);
            listViewUI.getRelativeLayout().setBackgroundResource(R.drawable.img_fucs_rel);
        } else {
            listViewUI.getImageView().setAlpha(0.4f);
            listViewUI.getImageView().setBackgroundResource(R.drawable.img_blus);
            listViewUI.getRelativeLayout().setBackgroundResource(R.mipmap.relbackblus);

        }
    }

    @Override
    public void onItemClick(View view, int focusIndex) {


        ListViewAdapter lvAdapter = (ListViewAdapter) listViewUI.getListViewVScroll().getAdapter();
        IListViewShowItem item = lvAdapter.getListViewShowItem(focusIndex);
        Channel channel = GrapeContext.curGrapeContext().getChannelDLinkList().findChannelByNo(item.getSort());
        String channelID= GrapeContext.curGrapeContext().getChannelDLinkList().getChannelHis().getCurPlayChannel().getChannelID();
        if(channel!=null){
            String keyID =channel.getKeyID();
            if(!TextUtils.isEmpty(channelID)&&channelID.equals(keyID)) {
                GrapeContext.curGrapeContext().setChangingLive(false);
                return;
            }
        }


        item.itemClick(lvAdapter.getDataList(), focusIndex, listViewUI);
        //lvAdapter.notifyDataSetInvalidated();
        //lvAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemMove(View view, int focusIndex) {
        if(lastItem!=-1&&focusIndex!=lastItem)
            openMarquee(listViewUI,lastItem,false);

        ListViewAdapter lvAdapter = (ListViewAdapter) listViewUI.getListViewVScroll().getAdapter();
        IListViewShowItem item = lvAdapter.getListViewShowItem(focusIndex);
        if (this.listViewUI.getNextListViewUI() != null) {
            // 异步更新 缺点,有UI闪动  同步更新,缺点:UI表现不流程
            updateListAsync(item);
        }

        openMarquee(listViewUI,focusIndex,true);
        lastItem = focusIndex;
    }

    // 异步更新下级列表
    private void updateListAsync(IListViewShowItem item) {
        new ChListTask().execute(item, this.listViewUI);
    }


    // 同步更新下级节点
    private void updateList(IListViewShowItem item) {
        List<IListViewShowItem> chList = item.getChList();
        if (chList != null && chList.size() > 0) {
            this.listViewUI.getNextListViewUI().show();
            this.listViewUI.getNextListViewUI().refresh(chList);
        } else {
            this.listViewUI.getNextListViewUI().hide();
        }
    }
}
